﻿
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using TTQT;
using TTQT.TTQT_Code;
using TTQT.TTQT_Code.Info;

namespace TFC.Web
{

	/// <summary>
	///		Summary description for ThamSoHeThong.
	/// </summary>
    public partial class ThamSoHeThong : MasterControl
	{
		protected System.Web.UI.WebControls.Panel pnEdit;
		protected System.Web.UI.WebControls.Label lblParaName;
		protected System.Web.UI.WebControls.Label lblParaDesc;
		protected System.Web.UI.WebControls.TextBox txtParaValue;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.Button cmdAbort;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				bindSysVarGrid();
			}
		}
        
		private void bindSysVarGrid()
		{
			grSysVar.DataSource = DataAccessObject.instance().getAllSysVars();
				grSysVar.DataBind();
            //lay thong so san pham
            CommonDAO cm=new CommonDAO();
		    fck.Value=cm.chayCauSQLScalarString("select ThongSoSanPham from ThongSoSanPham");
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.grSysVar.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.grSysVar_ItemCommand);
			this.grSysVar.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grSysVar_ItemDataBound);

		}
		#endregion

		private void grSysVar_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if(e.CommandName.Equals("cmdSua"))
			{
				SysvarInfo info = DataAccessObject.instance().getSysVar(e.Item.Cells[1].Text.Trim());
				if(null!=info)
				{
					this.lblParaName.Text = info.ParaName;
					this.lblParaDesc.Text = info.ParaDesc;
					this.txtParaValue.Text = info.ParaValue;
				}
			}
			if(e.CommandName.Equals("cmdReset"))
			{
					this.lblParaName.Text = e.Item.Cells[1].Text;
					this.lblParaDesc.Text = e.Item.Cells[2].Text;
					this.txtParaValue.Text = "";// e.Item.Cells[3].Text;
			}
		}

		protected void cmdUpdate_Click(object sender, System.EventArgs e)
		{
			SysvarInfo info = new SysvarInfo();
			bool updateOk = true;
			foreach(DataGridItem etem in grSysVar.Items)
			{
				info.ParaName =etem.Cells[1].Text ;
				info.ParaDesc = etem.Cells[2].Text ;
                TextBox textValue = (TextBox)etem.FindControl("txtValue");
				if(textValue !=null)
				{
					info.ParaValue = textValue.Text;
				}
				try
				{
					if(DataAccessObject.instance().updateSysVar(info)<0)
					{
						updateOk = false;
						lblMessage.Text += "\nKhong update duoc " + info.ParaDesc;
					}
				}catch(Exception ex)
				{
				}
			}
			if(updateOk)
			{
				lblMessage.Text = "Cập nhật thành công!";
				lblMessage.Visible = true;
			}
		}
		
		private void grSysVar_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			//DataBinder.Eval(Container.DataItem,"ParaValue")
            TextBox txtValue = (TextBox)e.Item.FindControl("txtValue");
			if(null !=txtValue)
			{
				if(!e.Item.Cells[2].Text.ToUpper().StartsWith("USER") && !e.Item.Cells[2].Text.ToUpper().StartsWith("PASS"))
				{
					txtValue.TextMode = TextBoxMode.SingleLine;
				}
				txtValue.Text = e.Item.Cells[3].Text.Trim();
			}
			
//			if (e.Item.ItemIndex < 0) return;
//			
//			Button cmdGuiGiaoDich = (Button) e.Item.FindControl("cmdGuiGiaoDich");
//			if(cmdGuiGiaoDich != null) cmdGuiGiaoDich.Attributes.Add("onclick", "openPage('GiaoDich/GuiGiaoDichPage.aspx?tranCode="+e.Item.Cells[0].Text+"'); return false;");

			//e.Item.CssClass = "Status" + e.Item.Cells[1].Text; // Đặt màu nền cho dòng tùy theo trạng thái giao dịch
		}

		protected void cmdCancel_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(navigateUrl(0));
		}
        protected void cmdThongSo_Click(object sender, EventArgs e)
        {
            QuanTriDAO cm = new QuanTriDAO();
            cm.thongSoSanPham_update(fck.Value);
        }
}
}
